package controllers;

import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import common.FBSecure;

import models.User;
import models.Contact;

import play.data.validation.*;
import play.libs.Mail;
import play.mvc.Controller;
import play.mvc.With;

@With(FBSecure.class)
public class Contacts extends FBController{
	
	public static void contact(){
		
		setFbIdandName();

		User user = getUser();
		
		render(user);
	}
	
	public static void postContact( @Required String subject, @Required String message) throws EmailException{		

		
		setFbIdandName();

		User user = getUser();

		Contact contact = new Contact(subject, message);
		
		if (validation.hasErrors()) {
			renderTemplate("Contacts/contact.html", user, contact);
		}
		contact.save();
		//System.out.println(contact.getSubject());
		
		SimpleEmail email = new SimpleEmail();
		email.setFrom("goturbenigittiginyer@gmail.com");
		email.addTo("goturbenigittiginyer@gmail.com");
		email.setSubject(contact.getSubject());
		email.setMsg(user.getName() +"\n"+contact.getMessage());
			
		Mail.send(email);
		
		String msg = "Mesajınız oluşturuldu, Sizinle en kısa sürede iletişime geçeceğiz!!";
		
			renderTemplate("Contacts/contact.html", user, contact, msg);
	}

}
